﻿package mclight.containers;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.Lib;
import mclight.Component;
import mclight.ComponentSprite;
import mclight.IAutoSizable;

class VBox extends Container, implements IAutoSizable
{
	public function new()
	{
		super();
		_autoSize = true;
		gap = 10;
		_content = new Sprite();
		_sprite.addChild(_content);
	}
	
	public override function draw()
	{
		if (autoSize)
		{
			var maxWidth = .0;
			for (i in 0 ... _content.numChildren)
			{
				var target;
				var child = _content.getChildAt(i);
				var componentSprite = Lib.as(child, ComponentSprite);
				if (componentSprite != null)
				{
					target = componentSprite.component.bounds;
				}
				else
				{
					target = new Rectangle(child.x, child.y, child.width, child.height);
				}
				if (target.width > maxWidth)
				{
					maxWidth = target.width;
				}
			}
			var offsetY = .0;
			for (i in 0 ... _content.numChildren)
			{
				var target;
				var child = _content.getChildAt(i);
				var componentSprite = Lib.as(child, ComponentSprite);
				if (componentSprite != null)
				{
					componentSprite.component.width = maxWidth;
				}
				else
				{
					child.width = maxWidth;
				}
				child.x = 0;
				child.y = offsetY;
				offsetY += child.height + gap;
			}
		}
	}
	
	public var gap:Int;
	
	public var autoSize(getAutoSize, setAutoSize):Bool;
	var _autoSize:Bool;
	function getAutoSize()
	{
		return _autoSize;
	}
	function setAutoSize(value:Bool)
	{
		return _autoSize = value;
	}
}